#include "stdafx.h"
#include "CodeTool.h"
#include "DBHelper.h"
#include "DALUserInfo.h"
#include "Diabetes.h"
#include "FormLogin.h"
#include "FormMain.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

CFormLogin::CFormLogin(CWnd* pParent /*=NULL*/)
	: CDialog(CFormLogin::IDD, pParent)
{
	//{{AFX_DATA_INIT(CFormLogin)
	//}}AFX_DATA_INIT
	m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}

void CFormLogin::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CFormLogin)
	DDX_Control(pDX, IDC_EDITPASSWORD, m_edit_password);
	DDX_Control(pDX, IDC_EDITNAME, m_edit_name);
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CFormLogin, CDialog)
	//{{AFX_MSG_MAP(CFormLogin)
	ON_WM_PAINT()
	ON_WM_QUERYDRAGICON()
	ON_BN_CLICKED(IDC_BTNLOGIN, OnBtnLogin)
	ON_WM_LBUTTONDOWN()
	ON_WM_HELPINFO()
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

BOOL CFormLogin::OnInitDialog()
{
	CDialog::OnInitDialog();

	SetIcon(m_hIcon, TRUE);	
	SetIcon(m_hIcon, FALSE);
	
	::SetShapeWindowSkin(this->m_hWnd, "IDD_FORM_LOGIN");

	SetWindowText(CCodeTool::Xqm_GetAppName());
	m_edit_name.SetFocus();
	m_edit_password.SetPasswordChar('*');

	//--debug
	m_edit_name.SetWindowText("xqmsoft");
	m_edit_password.SetWindowText("1002310023");
	//

	return FALSE;
}

void CFormLogin::OnPaint() 
{
	if (IsIconic())
	{
		CPaintDC dc(this);

		SendMessage(WM_ICONERASEBKGND, (WPARAM) dc.GetSafeHdc(), 0);

		int cxIcon = GetSystemMetrics(SM_CXICON);
		int cyIcon = GetSystemMetrics(SM_CYICON);
		CRect rect;
		GetClientRect(&rect);
		int x = (rect.Width() - cxIcon + 1) / 2;
		int y = (rect.Height() - cyIcon + 1) / 2;

		dc.DrawIcon(x, y, m_hIcon);
	}
	else
	{
		CDialog::OnPaint();
	}
}

HCURSOR CFormLogin::OnQueryDragIcon()
{
	return (HCURSOR) m_hIcon;
}

void CFormLogin::OnBtnLogin() 
{
	CString szName,szPassword;
	m_edit_name.GetWindowText(szName);
	m_edit_password.GetWindowText(szPassword);
	if(szName.IsEmpty()||szPassword.IsEmpty())
	{
		return;
	}
	CDALUserInfo dal;
	if(dal.UserLogin(szName,szPassword))
	{
		ShowWindow(SW_HIDE);
		CFormMain main(szName);
		main.DoModal();
		OnOK();
	}
}

void CFormLogin::OnLButtonDown(UINT nFlags, CPoint point) 
{
	CDialog::OnLButtonDown(nFlags, point);
	PostMessage( WM_NCLBUTTONDOWN, HTCAPTION, MAKELPARAM( point.x, point.y));
}

BOOL CFormLogin::OnHelpInfo(HELPINFO* pHelpInfo) 
{
	return FALSE;
}